package de.rosstauscher.jculfw.homematic;

import de.rosstauscher.jculfw.IRawMessageProcessor;
import de.rosstauscher.jculfw.homematic.events.HomeMaticEvent;

/**********************************************************
 * Parser for Homematic/Asksin messages
 * Allnnccttssssssddddddpp...
 *   ll - length
 *   nn - msg counter
 *   cc - control byte
 *   tt - msg type
 *   ss - sender address
 *   dd - destination address 000000 for a broadcast message.  
 *   pp - payload
 *   
 *  The whole homematic procotol is really a mess and we support
 *  at the moment only a very small part of it.
 *   
 * @author Rossi java@rosstauscher.de
 *********************************************************/

public abstract class HomeMaticMessageProcessor implements IRawMessageProcessor {

	/* (non-Javadoc)
	 * @see de.rosstauscher.jculfw.IRawMessageProcessor#parseInput(java.lang.String)
	 */
	public final boolean parseInput(String data) {
		if (data.startsWith("A") && data.length() >= 12) {
			data = data.substring(1);
			String length = data.substring(0, 2);
			String msgCount = data.substring(2, 4);
			String cntlByte = data.substring(4, 6);
			String msgType = data.substring(6, 8);
			String sender = data.substring(8, 14);
			String dest = data.substring(14, 20);
			String payLoad = data.substring(20);
			
			processRaw(length, msgCount, cntlByte, msgType, sender, dest, payLoad);

			// now try to parse to an event for handling.
			parseAndProcessAsEvent(length, msgCount, cntlByte, msgType, sender, dest, payLoad);
			
			return true;
		}
		if (data.startsWith("a")) {
			// TODO binary encoded data not supported yet.
		}
		return false;
	}

	/**********************************************************
	 * The raw message to parse into events.
	 * @param length of the whole message as hex
	 * @param count message counter as hex
	 * @param control control byte as hex
	 * @param msgType the message type as hex.
	 * @param sender the sender of the message.
	 * @param destination the destination of the message.
	 * @param payLoad the message specific data as hex.
	 *********************************************************/
	
	private void parseAndProcessAsEvent(String length, String msgCount,
			String cntlByte, String msgType, String sender, String dest,
			String payLoad) {
		HomeMaticEvent evt = null;
		if (evt == null) {
			evt = HomeMaticUtil.parseRemoteCommand(length, msgCount, cntlByte, msgType, sender, dest, payLoad);
		}
		if (evt == null) { // Fallback to a event that holds only the raw data.
			evt = new HomeMaticEvent(length, msgCount, cntlByte, msgType, sender, dest, payLoad);
		}
		processEvent(evt);
	}
	
	/**********************************************************
	 * The raw message data to process.
	 * @param length of the whole message as hex
	 * @param count message counter as hex
	 * @param control control byte as hex
	 * @param msgType the message type as hex.
	 * @param sender the sender of the message.
	 * @param destination the destination of the message.
	 * @param payLoad the message specific data as hex.
	 *********************************************************/
	
	protected void processRaw(String length, String count, String control, String msgType, String sender, String destination, String payLoad) {
		// Override me to process the data raw data.
	}
	
	/**********************************************************
	 * Override me to process a parsed event.
	 * @param event to process.
	 *********************************************************/
	
	protected void processEvent(HomeMaticEvent event) {
		// override me to process a parsed event.
	}

}
